package cn.shujuhai.common.controller.menu;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.shujuhai.common.aop.SysLog;
import cn.shujuhai.common.domain.SysMenu;
import cn.shujuhai.common.pluging.data.ResultVo;
import cn.shujuhai.common.service.SysMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: NiuYaHu
 * @Date: 2024/12/5 12:01
 * @Description: 针对菜单的控制层操作
 **/
@RestController
@RequestMapping("/menu")
@RequiredArgsConstructor
public class SysMenuController {
    final SysMenuService sysMenuService;

    /**
     * 获取菜单列表
     *
     * @return 排序后的菜单列表
     */
    @SysLog(value = "获取菜单列表", operationType = "查询", module = "菜单模块")
    @GetMapping("/list")
    public ResultVo<List<SysMenu>> getMenuList() {
        return sysMenuService.getMenuList();
    }

    /**
     * 菜单的启用禁用
     *
     * @param menuId 菜单Id
     * @return
     */
    @SaCheckPermission("menu_enable")
    @SysLog(value = "菜单启用禁用", operationType = "编辑", module = "菜单模块")
    @PostMapping("/enable/{menuId}")
    public ResultVo<SysMenu> enable(@PathVariable("menuId") Long menuId) {
        return this.sysMenuService.enable(menuId);
    }

    /**
     * 根据菜单Id删除菜单
     *
     * @param menuId 菜单Id
     * @return 删除成功或失败的结果及响应信息
     */
    @SysLog(value = "删除菜单", operationType = "编辑", module = "菜单模块")
    @PostMapping("/delete/{menuId}")
    public ResultVo<SysMenu> removeMenuById(@PathVariable("menuId") Long menuId) {
        return this.sysMenuService.removeMenuById(menuId);
    }

    /**
     * 新增菜单
     *
     * @param sysMenu 新增菜单对象
     * @return 新增菜单对象
     */
    @SysLog(value = "新增菜单", operationType = "新增菜单", module = "菜单模块")
    @PostMapping("/add")
    public ResultVo<?> addMenu(@RequestBody @Validated SysMenu sysMenu) {
        return this.sysMenuService.addMenu(sysMenu);
    }

    /**
     * 根据菜单Id获取菜单
     *
     * @param menuId 菜单Id
     * @return 获取到的菜单对象
     */
    @SaCheckPermission("menu_list")
    @SysLog(value = "根据菜单Id获取菜单", operationType = "编辑", module = "菜单模块")
    @GetMapping("/getMenuByMenuId/{menuId}")
    public ResultVo<?> getMenuByMenuId(@PathVariable("menuId") Long menuId) {
        return this.sysMenuService.getMenuByMenuId(menuId);
    }

    /**
     * 菜单编辑
     *
     * @param sysMenu 更新菜单对象
     * @return 更新后的菜单对象
     */
    @SysLog(value = "编辑菜单", operationType = "编辑", module = "菜单模块")
    @PostMapping("/update")
    public ResultVo<?> updateMenu(@RequestBody @Validated SysMenu sysMenu) {
        return this.sysMenuService.updateMenu(sysMenu);
    }

    /**
     * 根据菜单Id获取权限Id
     *
     * @param menuId 菜单Id
     * @return 菜单对应的权限id集合
     */
    @SaCheckPermission("menu_list")
    @SysLog(value = "根据菜单Id获取权限Id", operationType = "查询", module = "菜单模块")
    @GetMapping("/getPermissionIdsByMenuId")
    public ResultVo<?> getPermissionIdsByMenuId(Long menuId) {
        return ResultVo.success(this.sysMenuService.getPermissionIdsByMenuId(menuId));
    }
}
